﻿namespace EyeOpen.ViewModel
{
    using System.Windows.Input;
    using EyeOpen.TimeAssistant.Client.BootStrapper;
    using EyeOpen.TimeAssitant.Client.Service;
    using EyeOpen.UI.Silverlight;

    public partial class UserViewModel
    {
        private readonly IAsynchronousAuthenticationService authenticateServiceAsynchronous =
                                                                new AuthenticateServiceAsynchronousFactory()
                                                                .CreateServiceContext();

        private readonly WindowManager<HandleErrorWindowEvent> errorWindowManager = new WindowManager<HandleErrorWindowEvent>();
        private readonly WindowManager<HandleLoginWindowEvent> loginWindowManager = new WindowManager<HandleLoginWindowEvent>();

        public UserViewModel()
        {
            AuthenticateUserCommand = new RelayCommand(PerformAuthenticateUser);
            authenticateServiceAsynchronous.AuthenticationLoaded += AsynchronousAuthenticationLoaded;

            LoginCommand = new RelayCommand(PerformLogin);
            CloseLoginWindowCommand = new RelayCommand(CloseLoginWindow);
            CloseErrorWindowCommand = new RelayCommand(CloseErrorWindow);
        }

        public ICommand AuthenticateUserCommand
        {
            get;
            private set;
        }

        public ICommand LoginCommand
        {
            get;
            private set;
        }

        public ICommand CloseLoginWindowCommand
        {
            get;
            private set;
        }

        public ICommand CloseErrorWindowCommand
        {
            get;
            private set;
        }

        private void PerformAuthenticateUser()
        {
            IsBusy = true;
            authenticateServiceAsynchronous.AuthenticateUser(User, Password);
        }

        private void AsynchronousAuthenticationLoaded(object sender, AuthenticateEventArgs e)
        {
            if (!e.Result)
            {
                IntroductoryText = "Error on login.";
                ErrorText = "Bad user or password.";

                errorWindowManager.HandleWindow(TheAggregator, false);
            }
            else
            {
                loginWindowManager.HandleWindow(TheAggregator, true);
                IsLoggedIn = true;
            }

            IsBusy = false;
            User = string.Empty;
            Password = string.Empty;
        }

        private void PerformLogin()
        {
            if (!IsLoggedIn)
            {
                loginWindowManager.HandleWindow(TheAggregator, false);
            }
            else
            {
                IsLoggedIn = false;
            }
        }

        private void CloseLoginWindow()
        {
            loginWindowManager.HandleWindow(TheAggregator, true);
        }

        private void CloseErrorWindow()
        {
            errorWindowManager.HandleWindow(TheAggregator, true);
        }
    }
}
